﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using Microsoft.Win32;

namespace SMS.Core
{
    /// <summary>
    /// 串口操作类
    /// </summary>
    public class CommOperation
    {
        #region 常量
        /// <summary>
        /// 回车、换行符
        /// </summary>
        public const string HUICHE_HUANHANG = "\r\n";
        /// <summary>
        /// 回车符
        /// </summary>
        public const string HUICHE = "\r";
        /// <summary>
        /// 普通AT命令延时
        /// </summary>
        public const int MAX_DELAY_TIME1 = 100;
        /// <summary>
        /// 特殊情况下AT命令最大延时
        /// </summary>
        public const int MAX_DELAY_TIME2 = 500;
        /// <summary>
        /// 最大重试次数
        /// </summary>
        public const int MAX_TRY_NUM = 3;
        #endregion

        #region 串口操作函数
        /// <summary>
        /// 获取本机的串口
        /// </summary>
        public List<string> GetCommList()
        {
            try
            {
                List<string> commList = new List<string>();
                RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");        //获取本机串口信息
                if (keyCom != null)
                {
                    string[] sSubKeys = keyCom.GetValueNames();
                    foreach (string sName in sSubKeys)
                    {
                        string sValue = (string)keyCom.GetValue(sName);
                        commList.Add(sValue);
                    }
                }
                return commList;
            }
            catch (Exception error)
            {
                //throw error;
                return null;
            }
        }

        /// <summary>
        /// 用来检测串口和模块是否连接
        /// </summary>
        /// <param name="comm"></param>
        /// <returns></returns>
        public bool CheckCommAndModemConn(SerialPort comm)
        {
            try
            {
                if (comm.IsOpen == true)          //获取一个值，该值指示 SerialPort 对象的打开或关闭状态。
                {
                    return true;
                }
                else
                {
                    comm.Open();
                    if (comm.IsOpen == true)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (SystemException er)
            {
                return false;
            }
        }
        /// <summary>
        /// 连接串口
        /// </summary>
        public bool MMSOpen(SerialPort MMScomm)
        {
            try
            {
                if (MMScomm.IsOpen == true)
                    MMScomm.Close();
                MMScomm.Open();
                return MMScomm.IsOpen;
            }
            catch (System.Exception err)
            {
                return false;
            }
        }
        /// <summary>
        /// 断开串口
        /// </summary>
        /// <param name="MMScomm"></param>
        /// <returns></returns>
        public bool MMSClose(SerialPort MMScomm)
        {
            try
            {
                if (MMScomm.IsOpen == true)
                    MMScomm.Close();
                return !MMScomm.IsOpen;
            }
            catch (System.Exception err)
            {
                return false;
            }
        }
        /// <summary>
        /// 向串口发送字符串内容
        /// </summary>
        /// <param name="strMsg"></param>
        /// <returns></returns>
        public bool MMSSend(SerialPort MMScomm, string strMsg)
        {
            try
            {
                MMScomm.Write(strMsg);
                return true;
            }
            catch (SystemException er)
            {
                return false;
            }
        }
        /// <summary>
        /// 向串口发送字节数组内容
        /// </summary>
        /// <param name="bytMsg"></param>
        /// <returns></returns>
        public bool MMSSend(SerialPort MMScomm, byte[] bytMsg)
        {
            try
            {
                MMScomm.Write(bytMsg, 0, bytMsg.Length);
                return true;
            }
            catch (SystemException er)
            {
                return false;
            }
        }
        #endregion
    }
}
